package com.group1.bookmanagement.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.group1.bookmanagement.action.BookAction;
import com.group1.bookmanagement.dao.BaseDao;
import com.group1.bookmanagement.domain.Photo;
import com.group1.bookmanagement.domain.Book;
import com.group1.bookmanagement.domain.Label;
import com.group1.bookmanagement.domain.PageBean;
import com.group1.bookmanagement.service.BookService;
import com.group1.bookmanagement.service.LabelService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("bookAction")
@Scope("prototype")
public class BookAction extends ActionSupport implements ModelDriven<Book> {

	private static final long serialVersionUID = 1L;

	@Resource(name = "bookService")
	private BookService bookService;
	@Resource(name = "labelService")
	private LabelService labelService;
	private Book book = new Book();

	private File file;
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		ActionContext context = ActionContext.getContext();
		HttpParameters params = context.getParameters();
		Parameter param_labelId = params.get("labelId");
		if (param_labelId.getValue() != null) {
			Long labelId = Long.valueOf(param_labelId.getValue());
			Label label = labelService.get(labelId);
			book.setLabel(label);
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		if (file != null) {
			Photo photo = new Photo(file);
			photo.upload(request);
			book.setCover(photo);
		}
		bookService.saveBook(book);
		return SUCCESS;
	}

	/**
	 * 删除
	 * @return
	 */
	public String delete() {
		ActionContext context = ActionContext.getContext();
		HttpParameters params = context.getParameters();
		Parameter param = params.get("id");
		Integer id = Integer.valueOf(param.getValue());
		Book book = bookService.getBook(id);
		bookService.deleteBook(book);

		return SUCCESS;
	}

	/**
	 * 编辑
	 * @return
	 */
	public String edit() {
		bookService.updateBook(book);
		return SUCCESS;
	}

	/**
	 * 查询
	 * @return
	 */
	public String get() {
		ActionContext context = ActionContext.getContext();
		HttpParameters params = context.getParameters();
		Parameter param = params.get("id");
		Integer id = Integer.valueOf(param.getValue());
		Book book = bookService.getBook(id);
		context.put("book", book);

		return SUCCESS;
	}

	/**
	 * 查询所有
	 * @return
	 */
	public String list() {
		ActionContext context = ActionContext.getContext();
		HttpParameters params = context.getParameters();
		Parameter param = params.get("Page");
		Integer Page = Integer.valueOf(param.getValue());
		PageBean<Book> pageBean = bookService.listBook(Page);
		context.put("pageBean", pageBean);

		return SUCCESS;
	}

	/**
	 * 按标签查询所有
	 * @return
	 */
	public String listByLabel() {
		ActionContext context = ActionContext.getContext();
		HttpParameters params = context.getParameters();
		//labelId
		Parameter param_label_id = params.get("labelId");
		Long labelId = Long.valueOf(param_label_id.getValue());
		Label label = labelService.get(labelId);

		//Page
		Parameter param_page = params.get("Page");
		Integer page = Integer.valueOf(param_page.getValue());
		
		PageBean<Book> pageBean = bookService.listByLabel(label, page);
		context.put("pageBean", pageBean);
		return SUCCESS;
	}
	
	/**
	 * 跳转到主页
	 * @return
	 */
	public String toIndex() {
		ActionContext context = ActionContext.getContext();
		List<Book> list = bookService.top(5, "createdAt", "desc");
		context.put("list", list);
		
		return SUCCESS;
	}

	@Override
	public Book getModel() {
		return book;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
}
